package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
)

// 拷贝文件 使用ioutil
func main() {
	copy("D:\\go_project\\golang_study\\test.txt")
}

func copy(filePath string) {
	file, err := ioutil.ReadFile(filePath)
	if err != nil {
		fmt.Println("read file failed ,err:", err)
		return
	}

	ioutil.WriteFile("newFile.txt", file, 0666)

}

func CopyFile(dstName, srcName string) (written int64, err error) {
	srcFile, err := os.Open(srcName)
	if err != nil {
		fmt.Printf("open %s failed, err:%v.\n", srcName, err)
		return
	}
	defer srcFile.Close()

	dstFile, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644)
	if err != nil {
		fmt.Printf("open %s failed, err:%v.\n", srcName, err)
		return
	}
	defer dstFile.Close()
	return io.Copy(dstFile, srcFile)
}
